package com.zhangkunhao.notes.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.Question;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.QuestionServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

/**
 * 问题的controller
 *
 * @author 20642
 */
@WebServlet("/question/*")
public class QuestionServlet extends BaseDaoServlet {
    private QuestionServiceImpl questionService = new QuestionServiceImpl();

    public void askQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收提出的问题的数据:使用post发请求不能前端method不能加s!!
        String params = request.getReader().readLine();

        //2.转为对象
        Question question = JSON.parseObject(params, Question.class);

        // 通过Session接收用户Id
        Integer id = (Integer) request.getSession().getAttribute("userId");

        //3.修改id
        question.setPublishUserId(id);
        System.out.println(question + "控制层的question");

        //4.调用service层
        Message<Question> askQuestionMessage = questionService.askQuestion(question);
        System.out.println("askQuestionMessage:" + askQuestionMessage);

        //5.转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(askQuestionMessage));
    }

    public void thumpsUp(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收data请求体的数据
        String params = request.getReader().readLine();
        //2.转为对象
        Question question = JSON.parseObject(params, Question.class);

        //3.调用service层
        Message<Question> thumpsUpMessage = questionService.thumpsUp(question);
        System.out.println("thumpsUpMessage:" + thumpsUpMessage);

        //4.转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(thumpsUpMessage));
    }

    public void reportQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //乱码解决
        int reportQuestionId = Integer.parseInt(new String(request.getParameter("reportQuestionId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        String reportQuestionReason = new String(request.getParameter("reportQuestionReason").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        //调用service层
        Message<Question> reportQuestionMessage = questionService.reportQuestion(reportQuestionId, reportQuestionReason);

        // 转换为json数据，写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(reportQuestionMessage));
    }

    public void sendRejectQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //乱码解决
        int rejectQuestionId = Integer.parseInt(new String(request.getParameter("rejectQuestionId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        String rejectReason = new String(request.getParameter("rejectPublishQuestionReason").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        //调用service层
        Message<Question> rejectQuestion = questionService.sendRejectQuestion(rejectQuestionId, rejectReason);

        // 转换为json数据，写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(rejectQuestion));
    }

    public void agreedPublishQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //乱码解决
        int agreedQuestionId = Integer.parseInt(new String(request.getParameter("agreedQuestionId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));

        //调用service层
        Message<Question> questionMessage = questionService.agreedPublishQuestion(agreedQuestionId);

        // 转换为json数据，写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionMessage));
    }

    /**
     * 管理员同意问题举报
     *
     * @param request：
     * @param response：
     * @throws Exception：
     */
    public void agreedReportQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //乱码解决
        int agreedReportQuestionId = Integer.parseInt(new String(request.getParameter("agreedReportQuestionId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));

        //调用service层
        Message<Question> questionMessage = questionService.agreedReportQuestion(agreedReportQuestionId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionMessage));
    }

    public void disagreedReportQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //乱码解决
        int disagreedReportQuestionId = Integer.parseInt(new String(request.getParameter("disagreedReportQuestionId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));

        //调用service层
        Message<Question> questionMessage = questionService.disagreedReportQuestion(disagreedReportQuestionId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionMessage));
    }

    public void showNotPassQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service
        PageBean<Question> questionPageBean = questionService.showNotPassQuestion(userId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void showReportedQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service
        PageBean<Question> questionPageBean = questionService.showReportedQuestion(userId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void listUncheckedQuestions(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //调用service层,封装返回前端的对象
        PageBean<Question> questionPageBean = questionService.listUncheckedQuestions();

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void listReportedQuestions(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //调用service层,封装返回前端的对象
        PageBean<Question> questionPageBean = questionService.listReportedQuestions();

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void searchQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据,根据用户名搜索
        String myInterestedQuestion = new String(request.getParameter("myInterestedQuestion").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        //调用service层,封装返回前端的对象
        PageBean<Question> questionPageBean = questionService.searchQuestion(myInterestedQuestion);

        //3.写数据,转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }
}
